<?php

class handle_string {

    /**
     * str_quotes_to_entities
     *
     * Removes single and double quotes from a string
     *
     * @access	public
     * @param	string
     * @return	string
     */
    function str_quotes_to_entities($str) {
        return str_replace(array("\'", "\"", "'", '"'), array("&#39;", "&quot;", "&#39;", "&quot;"), $str);
    }

    /**
     * str_strip_slashes
     *
     * Removes slashes contained in a string or in an array
     *
     * @access	public
     * @param	mixed	string or array
     * @return	mixed	string or array
     */
    public function str_strip_slashes($str) {
        if (is_array($str)) {
            foreach ($str as $key => $val) {
                $str[$key] = str_strip_slashes($val);
            }
        } else {
            $str = stripslashes($str);
        }

        return $str;
    }

    public function str_get_description($string) {
        $string = $this->str_cut($string, 160);
        $string = trim(str_replace(
                        array('"', "'", "\r\n", "\n", "\t"), "", strip_tags($string)
                ));

        return $string;
    }

    public function str_cut($string, $lenth) {
        $string = strip_tags($string);
        $strLen = mb_strlen($string, 'UTF-8');
        $strShort = mb_substr($string, 0, $lenth, 'UTF-8') . "..";
        $strFull = ($strLen > $lenth) ? $strShort : $string;
        return $strFull;
    }

    public function str_short($str, $len) {
        $arr = explode("<hr />", $str);
        if (count($arr) > 1) {
            $strFull = strip_tags($arr[0]) . " ...";
        } else {
            $str = strip_tags($str);
            $strLenth = mb_strlen($str, "UTF-8");
            $strShort = mb_substr($str, 0, $len, "UTF-8") . " ...";
            $strFull = (($strLenth > $len) ? $strShort : $str);
        }
        $strFull = nl2br($strFull);
        return $strFull;
    }

    public function str_full($str)
    {
    }

    public function str_seokey($str)
    {
        $str = $this->str_clear_utf8($str);
        $str = $this->str_clear_special($str);
        $str = strtolower($str);
        return $str;
    }

    public function str_clear_utf8($str) {
        if (!$str)
            return false;
        $utf8 = array(
            'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
            'd' => 'đ|Đ',
            'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
            'i' => 'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
            'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
            'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
            'y' => 'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
        );
        foreach ($utf8 as $ascii => $uni)
            $str = preg_replace("/($uni)/i", $ascii, $str);
        return $str;
    }

    public function str_clear_special($text)
	{
        $text = strtolower($text);
        $text = str_replace("ß", "ss", $text);
        $text = str_replace("%", "", $text);
        $text = preg_replace("/[^_a-zA-Z0-9 -]/", "", $text);
        $text = str_replace(array('%20', ' '), '-', $text);
        $text = str_replace("-----", "-", $text);
		$text = str_replace("----", "-", $text);
        $text = str_replace("---", "-", $text);
        $text = str_replace("--", "-", $text);
        return $text;
    }

    public static function str_random($len = 8, $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
        $end = strlen($characters);

        $string = "";

        for ($p = 0; $p < $len; $p++) {
            $string .= $characters[mt_rand(0, $end)];
        }
        return $string;
    }

}